﻿using GalaSoft.MvvmLight.Threading;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TYBase;
using TYControl;
using TYPLC;
using TYPLC.PLC;

namespace TYComm_For_DSApp.View
{
    public partial class ViewPLCControl: WindowT
    {
        private ViewModel_Data_Control vm = new ViewModel_Data_Control();
        private readonly IPLCConnector plcConnector;
        private readonly List<GroupPara> groupParas;

        private void AddLog(string message, bool showMBox = false)
        {
            DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                edit.AppendText($"{DateTime.Now.ToString("HH:mm:ss")} - {message}{Environment.NewLine}");
                if (showMBox)
                {
                    MessageBox.Show(message);
                    return;
                }
            });
        }

        public ViewPLCControl(IPLCConnector opc, List<GroupPara> groupParas)
        {
            InitializeComponent();
            this.DataContext = vm;
            this.plcConnector = opc;
            this.groupParas = groupParas;
            this.Loaded += View_PLC_Data_Control_Loaded;
        }

        private void View_PLC_Data_Control_Loaded(object sender, RoutedEventArgs e)
        {
            vm.ListGroup.Clear();
            foreach (var groupItem in groupParas)
            {
                PLCGroupModel groupModel = new PLCGroupModel() { GroupName = groupItem.GroupName };
                foreach (var tagItem in groupItem.ListTag)
                {
                    groupModel.ListTags.Add(
                        new PLCTagModel()
                        {
                            Index = groupModel.ListTags.Count + 1,
                            Address = tagItem.Address,
                            DataLength = tagItem.DataLength,
                            DataType = tagItem.DataType,
                            TagIndex = tagItem.DataIndex,
                            TagName = tagItem.TagName
                        });
                }
                vm.ListGroup.Add(groupModel);
            }
        }

        private async void BtnClick_ReadPLC(object sender, RoutedEventArgs e)
        {
            if (vm.SelectedGroup == null)
                return;
            if (plcConnector == null)
                return;
            string groupName = vm.SelectedGroup.GroupName;
            var dx = (sender as Button).DataContext as PLCTagModel;
            if (dx != null)
            {
                object data = null;
                var ret = await Run(() =>
                {
                    return plcConnector.Read(groupName, dx.TagIndex, out data);
                });
                if (dx.DataLength <= 1)
                {
                    AddLog($"读取 {groupName} {dx.Address}  操作 {ret.AsString("成功", "失败")}   值:{data}");
                }
                else
                {
                    switch (dx.DataType)
                    {
                        case PLCDataType.BOOL:
                            AddLog($"读取 {groupName} {dx.Address}  操作 {ret.AsString("成功", "失败")}  值:{string.Join(",", (bool[])data)}");
                            break;

                        case PLCDataType.INT:
                            AddLog($"读取 {groupName} {dx.Address}  操作 {ret.AsString("成功", "失败")}   值:{string.Join(",", (int[])data)}");
                            break;

                        default:
                            break;
                    }
                }
            }
        }

        private async void BtnClick_WritePLC(object sender, RoutedEventArgs e)
        {
            if (vm.SelectedGroup == null)
                return;
            if (plcConnector == null)
                return;
            string groupName = vm.SelectedGroup.GroupName;
            var dx = (sender as Button).DataContext as PLCTagModel;
            if (dx != null)
            {
                if (dx.DataLength <= 1)
                {
                    switch (dx.DataType)
                    {
                        case PLCDataType.BOOL:
                            View_PLC_Control_SendBool view_send_bool = new View_PLC_Control_SendBool();
                            if (view_send_bool.ShowDialog() == true)
                            {
                                bool send_data = view_send_bool.SendData_Bool;
                                var ret = await Run(() =>
                                {
                                    return plcConnector.Write(groupName, dx.TagIndex, send_data);
                                });
                                AddLog($"发送 {groupName} {dx.Address} 值:{send_data} 操作 {ret.AsString("成功", "失败")} ");
                            }
                            break;

                        case PLCDataType.INT:
                            View_PLC_Control_SendInt view_send_int = new View_PLC_Control_SendInt();
                            if (view_send_int.ShowDialog() == true)
                            {
                                var send_data = view_send_int.SendData_Bool;
                                var ret = await Run(() =>
                                {
                                    return plcConnector.Write(groupName, dx.TagIndex, send_data);
                                });
                                AddLog($"发送 {groupName} {dx.Address} 值:{send_data} 操作 {ret.AsString("成功", "失败")} ");
                            }
                            break;
                        case PLCDataType.FLOAT:
                            View_PLC_Control_SendFloat view_send_float = new View_PLC_Control_SendFloat();
                            if (view_send_float.ShowDialog() == true)
                            {
                                var send_data = view_send_float.SendData_Bool;
                                var ret = await Run(() =>
                                {
                                    return plcConnector.Write(groupName, dx.TagIndex, send_data);
                                });
                                AddLog($"发送 {groupName} {dx.Address} 值:{send_data} 操作 {ret.AsString("成功", "失败")} ");
                            }
                            break;

                        case PLCDataType.String:
                            ViewEditString viewString = new ViewEditString();
                            if (viewString.ShowDialog() == true)
                            {
                                var send_data = viewString.Data;
                                var ret = await Run(() =>
                                {
                                    return plcConnector.Write(groupName, dx.TagIndex, send_data);
                                });
                                AddLog($"发送 {groupName} {dx.Address} 值:{send_data} 操作 {ret.AsString("成功", "失败")} ");
                            }
                            break;

                        default:
                            break;
                    }
                }
                else
                {
                    View_PLC_Control_SendArray view_send_bool = new View_PLC_Control_SendArray(dx.DataType, dx.DataLength);
                    if (view_send_bool.ShowDialog() == true)
                    {
                        var send_data = view_send_bool.SendData;
                        var ret = await Run(() =>
                        {
                            return plcConnector.Write(groupName, dx.TagIndex, send_data);
                        });
                        AddLog($"发送 {groupName} {dx.Address} 值:{string.Join(",", send_data)} 操作 {ret.AsString("成功", "失败")} ");
                    }
                }
            }
        }
    }
}